/**
 * 
 */
package com.ibuonline.search.solr.loader;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.dic.Dictionary;

/**
 * 搜索词库加载器
 * 
 * @author Liuye
 * 
 */
public class SearchWordLoader extends AbstractDbLoader {
	private static int rowNum = 0;
	private String sql = "SELECT id,word FROM search_term WHERE id>=?";

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ibuonline.search.solr.loader.AbstractDbLoader#getSql()
	 */
	@Override
	protected String getSql() {
		return sql.replace("?", String.valueOf(rowNum));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ibuonline.search.solr.loader.AbstractDbLoader#proecessResultSet(java
	 * .sql.ResultSet)
	 */
	@Override
	protected void proecessResultSet(ResultSet rs) throws SQLException {
		final Set<String> words = new HashSet<String>(1024);
		while (rs.next()) {
			String keyword = rs.getString("word");
			if (keyword != null && keyword.trim().length() > 0) {
				words.add(keyword);
			}
			rowNum = rs.getInt("id");
		}
		// 更新Ik的扩展词库
		Dictionary.initial(Configuration.getInstance());
		Dictionary.getSingleton().addWords(words);
	}

	/**
	 * @return the rowNum
	 */
	public int getRowNum() {
		return rowNum;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ibuonline.search.solr.loader.AbstractDbLoader#getJobName()
	 */
	@Override
	protected String getJobName() {
		return "搜索词库";
	}

}
